package com.alcloud.springplus.service.impl;

import com.alcloud.springplus.base.GenericServiceImpl;
import com.alcloud.springplus.base.ResponseCode;
import com.alcloud.springplus.domain.Dict;
import com.alcloud.springplus.exception.ApplicationException;
import com.alcloud.springplus.repository.DictDao;
import com.alcloud.springplus.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @className: DictServiceImpl
 * @description: 数据字典Service实现类
 * @date: 2018/12/10 14:10
 * @author: AllG
 * @updateRemark: 更新说明
 */
@Service("dictService")
public class DictServiceImpl extends GenericServiceImpl<Dict, Long> implements DictService {

    private final static Logger logger = LoggerFactory.getLogger(DictServiceImpl.class);

    private DictDao dictDao;

    public DictServiceImpl(DictDao dictDao) {
        super(dictDao);
        this.dictDao = dictDao;
    }

    /**
     * @param dict
     * @return java.util.List<com.alcloud.springplus.dto.Dict>
     * @method findAllByParentId
     * @description 根据父Id查询所有一级子集合
     * @date: 2018/12/11 14:15
     * @author: AllG
     */
    private List<Dict> findAllByParentId(Dict dict) {
        return dictDao.getAllByParent(dict);
    }

    /**
     * @param dict
     * @return java.util.List<com.alcloud.springplus.dto.Dict>
     * @method findAllChildrenByParentId
     * @description 循环查询所有的子集合
     * @date: 2018/12/11 14:40
     * @author: AllG
     */
    private List<Dict> findAllChildrenByParentId(Dict dict) {
        List<Dict> list = findAllByParentId(dict);
        if (null != list && !list.isEmpty()) {
            /**迭代*/
            list.forEach(a-> {
                a.setChildren(findAllChildrenByParentId(a));
                /**防止页面输出循环，子集节点取消父节点信息*/
                a.setParent(null);
            });
            return list;
        } else {
            return Collections.emptyList();
        }
    }


    /**
     * @method  findAllWithChildren
     * @description 条件查询子节点
     * @date: 2018/12/12 9:14
     * @author: AllG
     * @param params
     * @param pageable
     * @return org.springframework.data.domain.Page<com.alcloud.springplus.domain.Dict>
     */
    @Override
    public Page<Dict> findAllWithChildren(Map<String, String[]> params, Pageable pageable) {
        Page<Dict> result = findAll(params,pageable);
        try {
            if (null != result.getContent() && !result.getContent().isEmpty()) {
                result.getContent().forEach(dict->{
                    dict.setParent(null);
                    dict.setChildren(findAllChildrenByParentId(dict));
                });
            }
        } catch (Exception e) {
            logger.error(ResponseCode.SELECT_PAGINATION_EXCEPTION.getMessage(), e);
            throw new ApplicationException(ResponseCode.SELECT_PAGINATION_EXCEPTION);
        }
        return result;
    }
}
